<?php

class RankingController extends D_Controller {
	private $_top;
	public $cralwer_obj;
	public $website_obj;
	public $crawl_website_url_obj;
	public $keyword_obj;
	public $data_obj;
	public $page_obj;
	
	function init() {
		set_time_limit(8889889);
		$this->setLayout ('main', '' );
		$this->cralwer_obj = new Crawler();
		$this->website_obj = new Website();
		$this->crawl_website_url_obj = new CrawlerWebsiteUrl();
		$this->keyword_obj = new Keyword();
		$this->data_obj = new Data();
		$this->page_obj = new Page();
	}
	function allAction(){
		
	}
	
	//URL: ranking/website_page?history_id=103&keyword_id=25&website_id=1
	//
	function websitepageAction(){
		$params = $this->_request->getParams();
		$history_id = $params['history_id'];
		$keyword_id = $params['keyword_id'];
		$website_id = $params['website_id'];
		
		if (empty($params["page"])){
			$params["page"] = 1;
		}
		
		$website_page_ranking_obj = new WebsitePageRanking();
		$keyword_obj = new Keyword();
		$keyword_history_obj = new KeywordRankingHistory();
		$website_obj = new Website();
		
		//filter search
		$options = array();
		$where = array();
		$paginator = array();
		$where['history_id'] = $history_id;
		$where['keyword_id'] = $keyword_id;
		$where['website_id'] = $website_id;
		$paginator['page'] = $params["page"];
		$paginator['item_per_page'] = 10;
		$options['where'] = $where;
		$options['order'] = array('rank' => 'asc');
		$options['paginator'] = $paginator;
		
		$ranking_paginator = $website_page_ranking_obj->paginator($options);
		
		$this->view->rankings = $ranking_paginator['data'];
		$this->view->keyword = $keyword_obj->fetchRow($keyword_obj->select()->where("id=?",$keyword_id));
		$this->view->history = $keyword_history_obj->fetchRow($keyword_history_obj->select()->where("id=?",$history_id));
		$this->view->website = $website_obj->fetchRow($website_obj->select()->where("id=?",$website_id));
		
		$paginator_config = array();
		$paginator_config['total'] = $ranking_paginator['total'];
		$paginator_config['current_page'] = $params["page"];
		
		$this->view->paginator = D_Paging::init($paginator_config);
	}

	// Url: ranking/keyword
	function keywordAction() {
		$keyword_ranking_history_obj = new KeywordRankingHistory();
		$keyword_ranking_obj = new KeywordRanking();
		
		
		$histories = $keyword_ranking_history_obj->find_all(array('order' => 'created_at desc'));
		//$rankings=$keyword_ranking_history_obj->find($last_history->id)->current()->findDependentRowset("KeywordRanking");
		//$rankings = $keyword_ranking_history_obj->get_rankings($last_history->id);
		//$this->view->rankings = $rankings;
		$this->view->histories = $histories;
	}
	// AJAX CALL Url: ranking/keyword
	function loadkeywordrankingAction(){
		$this->setView('ranking/_load_keyword_ranking.phtml');
		$history_id = $this->_request->getParam('history_id');

		$keyword_ranking_history_obj = new KeywordRankingHistory();
		$cralwer_obj = new Crawler();
		
		$history = $keyword_ranking_history_obj->find($history_id)->current();
		$rankings = $keyword_ranking_history_obj->get_rankings($history_id);
		$crawler = $cralwer_obj->find($history->crawler_id)->current();
		$this->view->rankings = $rankings;
		$this->view->history  = $history;
		$this->view->crawler = $crawler;
	}
	
	
	// Url: ranking/website?history_id=xx&&keyword_id=yy
	function websiteAction() {
		$params = $this->_request->getParams();
		$history_id = $params['history_id'];
		$keyword_id = $params['keyword_id'];
		
		$website_ranking_obj = new WebsiteRanking();
		$keyword_obj = new Keyword();
		$keyword_history_obj = new KeywordRankingHistory();
		
		$ranking_sql = $website_ranking_obj->select()->where("history_id=?",$history_id)->where('keyword_id=?',$keyword_id);
		$this->view->rankings = $website_ranking_obj->fetchAll($ranking_sql);
		$this->view->keyword = $keyword_obj->fetchRow($keyword_obj->select()->where("id=?",$keyword_id));
		$this->view->history = $keyword_history_obj->fetchRow($keyword_history_obj->select()->where("id=?",$history_id));
	}
	
	// page ranking
	// Url: ranking/page?history_id=xx&&keyword_id=yy
	function pageAction() {
		$params = $this->_request->getParams();
		$history_id = $params['history_id'];
		$keyword_id = $params['keyword_id'];
		
		if (empty($params["page"])){ $params["page"] = 1;}
		
		$page_ranking_obj = new PageRanking();
		$keyword_obj = new Keyword();
		$keyword_history_obj = new KeywordRankingHistory();
		
		$options = array();
		$where = array();
		$paginator = array();
		$where['history_id'] = $history_id;
		$where['keyword_id'] = $keyword_id;
		$paginator['page'] = $params["page"];
		$paginator['item_per_page'] = 10;
		$options['where'] = $where;
		$options['order'] = array('rank' => 'asc');
		$options['paginator'] = $paginator;
		
		
		$ranking_paginator = $page_ranking_obj->paginator($options);
		$this->view->rankings = $ranking_paginator['data'];//$page_ranking_obj->fetchAll($ranking_sql);
		$this->view->keyword = $keyword_obj->fetchRow($keyword_obj->select()->where("id=?",$keyword_id));
		$this->view->history = $keyword_history_obj->fetchRow($keyword_history_obj->select()->where("id=?",$history_id));
		
		$paginator_config = array( 'total'=> 0);
		$paginator_config['total'] = $ranking_paginator['total'];
		$paginator_config['current_page'] = $params["page"];
		
		
		$this->view->paginator = D_Paging::init($paginator_config);
	
	}
	
	// AJ function
	function createkeywordrankingAction(){
		$this->noView();
		$last_crawler = $this->cralwer_obj->getLast();
		$keyword_ranking_history_obj = new KeywordRankingHistory();
		$keyword_ranking_history_obj->create_ranking(array("crawler_id" => $last_crawler->id));
		echo "Create successful.";
	}
	// AJ function
	function createwebsiterankingAction(){
		$this->noView();
		$last_crawler = $this->cralwer_obj->getLast();
		$website_ranking_history_obj = new WebsiteRankingHistory();
		$list_website = $last_crawler->findDependentRowset('CrawlerWebsite');
		foreach ($list_website as $website){
			//xep hang keyword tung website
			$website_ranking_history_obj->create_ranking(array("crawler_id" => $last_crawler->id, "website_id"=>$website->id));
		}
		
		echo "Create successful.";
	}
	
	
	//category website ranking
	function categorywebsiteAction(){
		$params = $this->_request->getParams();
		
		$cwr_obj = new CategoryWebsiteRankingHistory();
		$this->view->histories= $cwr_obj->find_all();
	}
	
	//sub kw cua 1 kw
	// vi du sub cua laptop co: laptop, mtxt, may tinh xach tay
	public function subkwAction(){
		$kw_id = $this->_request->getParam("id");
		if(isset($kw_id)){
			echo $kw_id;
			
		}
	}
	
	
	public function dump_loop($looo){
		foreach ($looo as $row){
			var_dump($row) ;
		}
	}
	
}

?>